{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python summary\n", "\n", "In this book, at several instances we use Python to collect, process, analyze and visualize data. We also use Python for some simulations. As not all are familiar with Python programming language, here are some basics.\n", "\n", "## Libraries\n", "We use some libraries with inbuilt functions which makes our script easier to code as well as more readable. Most often we use\n", "* **numpy** for various scientific calculations\n", "* **matplotlib** for plotting our data\n", "* **scipy** for least square regression\n", "\n", "## Save and process data\n", "We can 'store' data in an array. Each of the values in that array can be processed, plotted, printed and so on. In the example below we have an array named $a$. Some values are stored. We print these values, change one of them and print again the values (you can run the code cell by clicking on the {fa}`rocket` at the top right cornet and click on live code)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "a = np.array([0.0, 1.0, 1.2, 0.8, 0.5]) #making an array with some values.\n", "print(a) #printing the values.\n", "a[1] = 2.0 #changing the value of the second element.\n", "print(a) #printing the values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As shown above, to print values, we use the `print` command. \n", "\n", "With an array filled with data, we can do some statistic calculations:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_mean = np.mean(a) #Calculating the mean value of a\n", "a_std = np.std(a,ddof=1) #Calculating the standard deviation of a \n", "a_unc = np.std(a,ddof=1)/np.sqrt(len(a)) #Calculating the standard error of a\n", "\n", "print(\"Mean value of a is: \",a_mean) #printing the mean value of a\n", "print(\"Standard deviation of a is: \",a_std) #printing the standard deviation of a\n", "print(\"Standard error of a is: \",a_unc) #printing the standard error of a\n", "\n", "#more neatly:\n", "print(\"Standard error of a is: \", round(a_unc,1)) #printing the standard error of a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions\n", "You often use a piece of code several times. Sometimes with different values. If that happens, it is useful to write a function yourself. You must define a **function** and it is given a specific name. After the function name you specify the input variables. The variables inside the function are only temporarily stored inside the function temp_var and therefore cannot be used outside the function. This is called a local variable.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sum_function(i1, i2): #this sum_function requires two input variables\n", " temp_var = i1 + i2 #these input variables are added together and stored in a temporary variable\n", " return temp_var #that stored value is returned.\n", "\n", "x = sum_function(3, 5) \n", "\n", "print(x) # Dit print 8\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now try it yourselves using a product function..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def prod_function(i1, i2): #this prod_function requires two input variables\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting\n", "\n", "Often we want a visual representation of our data. In Python we can easily plot our data using the library matplotlib. This library support various plots, where we will show a few below. However, many more examples can be found here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Histogram\n", "\n", "A histogram shows how often a value or a certain range occurs in a data set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt #Importing the matplotlib library\n", "\n", "# Making a data set with a normal distribution\n", "data = np.random.normal(0,5,1000)\n", "plt.figure(num='histfig') # Create a new figure\n", "\n", "plt.hist(data,bins='auto',density=1, label='Occurence') # Plot the data\n", "\n", "plt.xlabel('Values') #Label for the x-as\n", "plt.ylabel('Probability') #Label for the y-as\n", "plt.legend() #Including a legend\n", "plt.show() #Show the plot\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatterplot\n", "\n", "Measurements in a dataset can be displayed in a scatter plot. Only the points are plotted (black in color here `k.`) and no line is drawn between the points. Below is an example of how a scatter plot can be made. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "x = [1, 2, 3, 4, 5]\n", "y = [1.1, 4.3, 9.2, 16.1, 25.1]\n", "\n", "plt.figure(num='figscatterplot')\n", "\n", "plt.plot(x,y,'k.', label='Measurements ')\n", "\n", "#enhancing the visuals\n", "plt.xlabel('Input voltage [V]')\n", "plt.ylabel('Output voltage [V]')\n", "plt.legend()\n", "plt.grid()\n", "plt.xlim(0,6)\n", "\n", "plt.show()\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Curve fitting\n", "\n", "To find the line of best fit, we can use `curve_fit` from the **scipy** library (it uses a least square regression). This function requires three input values: The function it should fit to, the $x$-values and the $y$-values The $x$ and $y$ values are arrays with your measurements.\n", "\n", "The `curve_fit` returns two variables, the values and covariance. The variable values_funcfit is an array with the optimal values ​​of the parameters for the fitted function.\n", "\n", "covariance is a 2d array containing the covariance of the fitted parameters. The values ​​on the diagonal indicate the variation of the fitted parameters. The standard error of the function with fitted parameters can be calculated as follows: `np.sqrt(np.diag(covariance))`. (Note! This is only a crash course Python. More information on data-analysis, measurement uncertainties and Python can be easily found on the web)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.optimize import curve_fit\n", "\n", "def fit_function(x,a,b):\n", " return x**a+b\n", "\n", "values, covariance = curve_fit(fit_function, x, y)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_test = np.linspace(0.8*min(x),1.2*max(x),1000) #creating a test array for the x-values\n", "y_fit = fit_function(x_test,values[0],values[1]) #calculating the y-values for the test array\n", "\n", "plt.figure(num='figscatterplot')\n", "\n", "plt.plot(x,y,'k.', label='Measurements ')\n", "plt.plot(x_test,y_fit,'r--', label='Fit')\n", "\n", "#enhancing the visuals\n", "plt.xlabel('Input voltage [V]')\n", "plt.ylabel('Output voltage [V]')\n", "plt.legend()\n", "plt.grid()\n", "plt.xlim(0,6)\n", "\n", "plt.show()\n", "\n", "print(\"a = \",values[0])\n", "print(\"b = \",values[1])" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.17" } }, "nbformat": 4, "nbformat_minor": 2 }